import moment from 'moment';

//断线重连时间
const reconnectionTime = 5 * 1000;
//心跳间隔 最迟60秒
const heartbeatTime = 50 * 1000;
const WSS_URL = 'wss://www.meritocean.cn/wss';
let websock = ''
import Vue from 'vue'
import { ACCESS_TOKEN, CHANGRPROR } from '@/store/mutation-types'
import { getNowTime } from '@/utils/util.js'


/**建立连接 */
export function initWebSocket() {
	if (!websock) {
		console.log('建立websocket连接')
		websock = new WebSocket(WSS_URL)
		websock.onmessage = websocketonmessage;
		websock.onopen = websocketonopen;
		websock.onerror = websocketonerror;
		websock.onclose = websocketclose;
	} else {
		console.log('websocket已连接')
	}

}

export function websocketonopen() {
	//连接建立之后执行校验
	websocketsend(Vue.ls.get(ACCESS_TOKEN));
	// websocketsend('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb21wYW55X2lkIjoiMjAyMDAzMTZhYjg4YWQwMWMyNzhkZThhMmRmYTg5MDI5ZWEyMTRhNSIsInVzZXJfaWQiOiIyMDIwMDMxNjM3ZDZiNDNhNDM5NTQzNmY1MjI0ZjgzNjQ5NDE0MDYxIiwidXNlcl9uYW1lIjoiMTg3MDU5MTAxMDgiLCJzY29wZSI6WyJzZXJ2aWNlIl0sImV4cCI6MTU5MDA2MjI2NSwiYXV0aG9yaXRpZXMiOlsiUGFnZTpTdG9ja1RyYW5zZmVyTWFuYWdlIiwiUGFnZTpNb25leVJlcG9ydEluZGV4IiwiUGFnZTpPdGhlckN1c3RvbWVyTWFuYWdlIiwiUGFnZTpDb250cmFjdE1hbmFnZSIsIlBhZ2U6Rmxsb3dNYW5hZ2UiLCJQYWdlOlN0b2NrTWFuYWdlIiwiTWV0aG9kOlVzZXI6QWRtaW4iLCJQYWdlOlN0b2NrRW50ZXJEZXRhaWxNYW5hZ2UiLCJQYWdlOlNhbGVJbmNvbWVJbnZvaWNlTWFuYWdlIiwiUGFnZTpJbnZvaWNlTWFuYWdlIiwiTWV0aG9kOlVzZXI6QWRkVXNlckJ5Q29tcGFueSIs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WxhbmNlSW5kZXgiLCJQYWdlOkhvbWVNYW5hZ2UiLCJQYWdlOlN0YWZmTWFuYWdlIiwiUGFnZTpTYWxlQWdyZWVtZW50TWFuYWdlIiwiUGFnZTpCYXNpY1NldHVwIiwiUGFnZTpDdXN0b21lck1hbmFnZSIsIlBhZ2U6U2FsZU1hbmFnZSIsIlBhZ2U6U3RvY2tFbnRlclNhbGVEZXRhaWxNYW5hZ2UiLCJQYWdlOlNhbGVQdXRIb3VzZU1hbmFnZSIsIlBhZ2U6U3lzdGVtU2V0dGluZ01hbmFnZSIsIlBhZ2U6QnV5SW5jb21lSW52b2ljZU1hbmFnZSIsIlBhZ2U6QnVzaW5lc3NSZXBvcnRJbmRleCIsIlBhZ2U6SG91c2VNYW5hZ2UiLCJQYWdlOkludmVudG9yeUludm9pY2UiLCJQYWdlOlN0b2NrQW1vdW50TWFuYWdlIl0sImp0aSI6IjQzNGZkMTM3LTM0ZTktNGU4YS1iM2RhLTgxOWVhMzA2NGRhOCIsImNsaWVudF9pZCI6InN5c3RlbUFkbWluIn0.LcZA91JCM4yOlUoc38BdvrtDrMx9LNXl6feLxA-3P1Y');
	//链接建立后 定时发送心跳 保持长连接
	websocketsendheartbeat();
}
export function websocketonerror() {
	//连接建立失败重连
	if (this.initTimeout) {
		clearTimeout(this.initTimeout);
	}
	this.initTimeout = setTimeout(() => this.initWebSocket(), reconnectionTime);
}
export function websocketonmessage(e) {
	//数据接收
	console.log('数据接收996-699')
	// this.$router.go(0)
	this.$router.push({
		name: 'redirect',
	})
	// this.$store2.state._achievements = getNowTime()
	Vue.ls.set(CHANGRPROR, getNowTime(), 24 * 60 * 60 * 1000)
	// window.location.reload()
	// const redata = JSON.parse(e.data);
	// this.arr.push({ sender: '服务器返回消息', msg: e.data, date: moment().format('YYYY-MM-DD hh:mm:ss') });
}
export function websocketsend(Data) {
	//数据发送
	console.log('数据发送996-699')
	// this.arr.push({ sender: '客户端发送消息', msg: Data, date: moment().format('YYYY-MM-DD hh:mm:ss') });
	websock.send(Data);
	//重置心跳，减少不必要的消息
	websocketsendheartbeat();
}
export function websocketsendheartbeat() {
	//保持长连接心跳
	if (xxx) {
		clearTimeout(xxx);
	}

}
export function xxx() {
	heartbeatTimeout = setTimeout(() => {
		websocketsend('心跳');
		websocketsendheartbeat();
	}, 10 * 1000);
}
export function websocketclose(e) {
	//数据发送
	//关闭
	console.log('断开连接', e);
	///连接建立失败重连
	if (this.initTimeout) {
		clearTimeout(this.initTimeout);
	}
	this.initTimeout = setTimeout(() => this.initWebSocket(), reconnectionTime);
}